var maplet = null;
$(document).ready(function () {
    initMap();
    bindEvent();
});

function bindEvent() {
    $('#clear').click(function () {
        location.href = basePath + "/cnbdw/smapInfoAction!indexPage.action";
    });
}

function initMap() {
    maplet = new Maplet("mmap");
    maplet.DISABLE_DBCLICK_ZOOM=false;
    maplet.centerAndZoom(new MPoint(0, 0), 0);
    maplet.clickToCenter=false;
    maplet.allowDrawingDragMap=false;
    maplet.markerDragAnimation=false;
    
    maplet.showOverview(false,false);
    maplet.setAutoZoom();
   // maplet.setIwStdSize(500,300);
    document.getElementById('LayerScale').style.display = 'none';
    document.getElementById('ImgCopyright').style.display = 'none';
    $('span:contains("GS(2011)1617")').parent().hide();

    onWindowResize();

    window.onresize = function () {
        onWindowResize();
    };


    if (fences) {
        $.each(fences, function (index, fence) {
            var points = [];
            var geoText = fence.geoText;
            if (geoText) {
                var points = [];
                var pids = geoText.split(';');
                $.each(pids, function (pindex, pid) {
                    var mPoint = new MPoint(pid);
                    points.push(mPoint);

                });
                drawarea(points, fence);
            }

        })
    }
}


function onWindowResize() {
    $('#clear').css("left", $('#mmap').position().left);
    $('#clear').css("top", $('#mmap').position().top);

    var width = $(window).width();
    var height = $(window).height();

    maplet.resize(600, 600);
  //  maplet.resize(300, 200);
}

function drawarea(points, fence) {
    var html = [
        "<table style='width: 80%;'>",
        "<tr><td style='width: 50%;text-align: right;'>报警区域编码：</td><td>" + fence.code + "</td></tr>",
        "<tr><td style='width: 50%;text-align: right;'>报警区域名称：</td><td>" + fence.name + "</td></tr>",
        "<tr><td style='width: 50%;text-align: right;'>报警区所在地图：</td><td>" + smap.name + "</td></tr>",
        "</table>",
        "<div style='text-align: center;padding: 10px;margin-top: 10px;'>",
        "<input style='padding: 5px 10px 5px 10px;' type='button' name='editFence' onclick='editFence(\"" + fence.id + "\")' value='编辑'/>&nbsp;",
        "<input style='padding: 5px 10px 5px 10px;' type='button' name='deleteFence' onclick='deleteFence(\"" + fence.id + "\")' value='删除'/>",
        "</div>"
    ];

    var brush = new MBrush("green");
    brush.arrow = false;
    brush.style = 0;
    brush.stroke = 1;
    brush.fill = true;
    brush.bgcolor = "blue";
    brush.color = "red";
    brush.bgtransparency = 35;
    var polyline = new MPolyline(
        points,
        brush,
        new MInfoWindow(fence.name, html.join(''))
    );
    maplet.addOverlay(polyline);
}

function editFence(id) {
    location.href = basePath + "/cnbdw/fenceInfoAction!toEditPage.action?id=" + id;
}
function deleteFence(id) {
    if (window.confirm('提示: 是否确定删除该条记录？')) {
        $.ajax({
            url: basePath + '/cnbdw/fenceInfoAction!delete.action',
            dataType: 'json',
            data: {ids: id},
            success: function (data) {
                alert(data.returnMsg);
                if (data.returnFlag) {
                    location.reload();
                }
            }
        });

    }
}